/* -+- file  ./kf2/GUI/GeckoChatView.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;
using Habazie.kf2.Core;
using Habazie.kf2.Exposition;

namespace Habazie.kf2.GUI
{
	/// <summary>
	/// 	Chat history window based on Gecko rendering engine
	/// </summary>
	public class GeckoChatView : Gtk.Frame, IChatView
	{
		private Html m_Gecko;
		private string m_Name;
		private int m_MsgId = 0; 
		
		const string fancy_template = 
"		<div class=\"Box Box{3}\" id=\"msg{2}\">"+
"			<div class=\"Avatar Avatar{3}\">"+
"				{5}"+
"			</div>"+
"			<div class=\"Msg Msg{3}\">"+
"				<span class=\"Time\">{4}</span>"+
"				<h2>{0}</h2>"+
"				<p>{1}</p>"+
"			</div>"+
"		</div>"+
"		<script>reveal('msg{2}',0)</script>";
		
		public GeckoChatView(string name) : base ()
		{
			m_Name = name;
			Shadow = Gtk.ShadowType.In;
			ShadowType = Gtk.ShadowType.In;
			m_Gecko = new Html ();
			m_Gecko.StyleSheet = "fancy/fancy.css";
			m_Gecko.PageTitle = m_Name;
			Add (m_Gecko);
			m_Gecko.Realized += OnR;
			m_Gecko.Show ();
			Clear ();
		//	this.SizeAllocated += new Gtk.SizeAllocatedHandler (OnSizeAllocated);
		//	Realize ();
		//	Clear ();
		}
		
		public Gtk.Widget GetWidget ()
		{
			return this;
		}
		
		public bool CanReparent {
			get { return false; }
		}
		
		//static explicit operator int (IChatView view);
		public void Clear ()
		{
			Util.Debug ("GUI", "GeckoChatView.Clear ()");
		//	LoadUrl ("file:///home/przemek/kf2/kf2/data/chat.html");
		//	RenderData ("<html><body>", "file:///kaka", "text/html");
		//	AppendData ("<html><body>");

		//	OpenStream ("file:///home/przemek/kf2/kf2/data/", "text/html");
		//Console.WriteLine ("{0}","file://" + System.Environment.CurrentDirectory);
			Console.Error.WriteLine ("GeckoChatView.Clear ()");
//			m_Gecko.OpenStream ("file://" + System.Environment.CurrentDirectory+"/", "text/html");
//			m_Gecko.AppendData ("<?xml version=\"1.0\" encoding=\"utf-8\"?><html><head><link rel=\"stylesheet\" href=\"default.css\"></head><body><h1>"+m_Name+"</h1>");
			m_Gecko.Clear ();
			m_Gecko.AppendData ("<script>"+
"                       function reveal (id, num)"+
"                       {"+
"                               obj = document.getElementById (id);"+
"                               if (num <= 10) {"+
"                                                       obj.style.opacity = num / 10;"+
"                                       setTimeout (\"reveal ('\"+id+\"', \"+(num+1)+\");\", 100);"+
"                               }"+
"                               obj.scrollIntoView();"+
"                       }"+
"</script>");
		}
		
		protected virtual void OnR (object sender, EventArgs e)
		{
	//		Console.WriteLine ("aadfesrgrtszre");
	//		Clear ();
		}
		
		public void DisplayMessage (string from, DateTime date, bool incoming, string text, Habazie.kf2.Roster.Avatar a)
		{
			if (true) {
				string avatar = "&nbsp;";
				if (a != null && System.IO.File.Exists(a.ThumbnailFileName))
					avatar = "<img src=\"" + a.ThumbnailFileName + "\" alt=\"\" />";
				m_Gecko.AppendData (String.Format (fancy_template,
						from, text, m_MsgId++,
						incoming?"In":"Out", date.ToShortTimeString (), avatar));
			}
//			else {
//				m_Gecko.AppendData (String.Format (	" <div class=\"OuterBox {3}\" id=\"msg{2}\">"+
//                 						"       <h2>{0}</h2>"+
//                        				"		<div class=\"InnerBox\">"+
//                                		"			{1}"+
//                        				"		</div>"+
//                						"</div>"+
//                						"<script>reveal('msg{2}',0)</script>",
//									from, text, m_MsgId++, incoming?"Incoming":"Outcoming"));
//			}
		}
		
		public void ScrollToEnd ()
		{
		}
		
		
		public void DisplayEvent (string from, DateTime date, string text)
		{
			m_Gecko.AppendData (String.Format ("<div class=\"Notify\" id=\"msg{0}\"><p>{1}</p></div>",
									m_MsgId, text));
			m_Gecko.AppendData (String.Format ("<script>reveal('msg{0}',0)</script>",
									m_MsgId++));

		}
		
	//	void OnSizeAllocated (object o, Gtk.SizeAllocatedArgs args) {
	//		if (m_Gecko != null)
	//			m_Gecko.SizeAllocate (args.Allocation);
	//	}
	}
	
}
